package top.songxs.gmall.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.songxs.gmall.common.properties.ThreadPoolProperties;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfiguration {
    @Autowired
    private ThreadPoolProperties pro;

    @Value("${spring.application.name}")
    private String name;

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        return new ThreadPoolExecutor(
                pro.getCorePoolSize(), pro.getMaximumPoolSize(), pro.getKeepAliveTime(),
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(pro.getWorkQueueSize()),
                new ThreadFactory() {
                    int count = 0;
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("【"+name+"】"+"===>>"+"threadPool"+"===>:"+ count++);
                        return thread;
                    }
                },
                new ThreadPoolExecutor.AbortPolicy());
    }
}
